import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/7 11:02
 * @description 单调栈
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] price  = {10,1,1,6};
        solution.finalPrices(price);
    }
    public int[] finalPrices(int[] prices) {
        int n = prices.length;
        int[] ans = Arrays.copyOf(prices, n);
        Deque<Integer> stack = new ArrayDeque<>();
        for(int i = n - 1; i >= 0; i--) {
            while (!stack.isEmpty() && stack.peek() > prices[i]) {
                stack.pop();
            }
            ans[i] -= stack.isEmpty() ? 0 : stack.peek();
            stack.push(prices[i]);

        }
        return ans;
    }
}
